package net.synck.gcontacts.wizards;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;

import com.google.gdata.data.contacts.ContactEntry;
import com.google.gdata.data.extensions.Email;
import com.google.gdata.data.extensions.PhoneNumber;
import com.google.gdata.util.ServiceException;

public class MergeWizard extends Wizard {

	private MergeWizardPageOne one;
	private Object contacts; 
	
	public void setContacts(Object entries) {
		this.contacts = entries;
	}

	Object getContacts() {
		return contacts;
	}
	
	public MergeWizard() {
		super();
		setNeedsProgressMonitor(true);			
	}

	@Override
	public void addPages() {
		one = new MergeWizardPageOne();
		//one.setContacts(contacts);
		addPage(one);
	}

	
	@Override
	public boolean performFinish() {
		if (one.getPrimaryContact() != null) {
			ContactEntry primaryContact = one.getPrimaryContact();
			if (contacts instanceof IStructuredSelection) {					
					IStructuredSelection strucSelection = (IStructuredSelection) contacts;
				for (Iterator iterator = strucSelection.iterator(); iterator.hasNext();) {
					Object element = (Object) iterator.next();
					if (element instanceof ContactEntry) {
						ContactEntry contact = (ContactEntry) element;
						if (contact == primaryContact)
							continue;
						mergeContact(primaryContact, contact);
					}
				}
				
				try {
					primaryContact.update();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (ServiceException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				return true;
			}
		}
		return false;
	}

	private ContactEntry mergeContact(ContactEntry target, ContactEntry source) {
		
		//phone
		//List<PhoneNumber> phones = new ArrayList<PhoneNumber>();
		for (PhoneNumber sourcePhone : source.getPhoneNumbers()) {
			boolean add = true;
			for (PhoneNumber targetPhone : target.getPhoneNumbers()) {
				if (targetPhone.getPhoneNumber().equalsIgnoreCase(sourcePhone.getPhoneNumber())) {
					add = false;
					break;
				}
			}
			if (add) {
				PhoneNumber newPhoneNumber = new PhoneNumber();
				newPhoneNumber.setLabel(sourcePhone.getLabel());
				newPhoneNumber.setRel(sourcePhone.getRel());
				newPhoneNumber.setUri(sourcePhone.getUri());
				newPhoneNumber.setPhoneNumber(sourcePhone.getPhoneNumber());
				if (target.getPhoneNumbers().size() == 0)
					newPhoneNumber.setPrimary(true);
				target.addPhoneNumber(newPhoneNumber);
			}
		}
		
		/*
		for (Email sourceEmail : source.getEmailAddresses()) {
			boolean add = true;
			for (Email targetEmail : target.getEmailAddresses()) {
				if (targetEmail.getAddress().equalsIgnoreCase(sourceEmail.getAddress())) {
					add = false;
					break;
				}
			}
			if (add) {
				Email newEmail= new Email();
				newEmail.setLabel(sourceEmail.getLabel());
				newEmail.setRel(sourceEmail.getRel());
				//newEmail.setUri(sourceEmail.getUri());
				newEmail.setAddress(sourceEmail.getAddress());
				if (target.getEmailAddresses().size() == 0)
					newEmail.setPrimary(true);
				target.addEmailAddress(newEmail);
			}
		}
		*/
		
		
		return target;
	}
	
}
